package com.fau.wm.entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;
import org.json.simple.JSONAware;
import org.json.simple.JSONObject;

import com.fau.wm.util.SystemConfig;

/**
 * 药品入库表
 * @author liy
 * @since 2014年7月24日,下午7:03:43
 */
@Entity
@Table(name = "medical_storage")
public class MedicalStorage implements Serializable, JSONAware {

	private static final long serialVersionUID = 3511273764742955108L;

	private String id;
	
	@NotBlank(message = "药品编号不能为空！")
	@Length(max = 50, message = "药品编号长度需要在0和50之间")
	private String serialNum;						// 编号
	
	@Length(max = 85, message = "标题长度需要在0和85之间")
	private String title;							// 标题
	private String warehouseId;					    // 仓库id
	
	@NotBlank(message = "入库类型不能为空！")
	@Length(max = 10, message = "入库类型长度需要在0和10之间")
	private String type;							// 入库类型
	private String typeVal;					    	// 入库类型字典表值
	private String status = "3";				    // 入库状态 0待审核，1已入库，2审核未通过 3新建
	
	@NotNull(message = "入库时间不能为空！")
	private Date storageTime;			    		// 入库时间
	private String writeUserId;				    	// 填写人id
	private String auditUserId;			        	// 审核人id
	private Date createTime = new Date();	        // 创建时间
	private Date updateTime = new Date();           // 更新时间
	
	private MedicalWarehouse warehouse;             // 仓库实体
	private String warehouseName;			    	// 仓库名称
	private MedicalAdmin writeUser;			        // 填单用户
	private MedicalAdmin auditUser;			        // 审核用户
	private String hospitalId;						//医院标识id

	private String filePath;					// 导入文件上传路径
	@Id
	@GenericGenerator(name = "generator", strategy = "uuid.hex")
	@GeneratedValue(generator = "generator")
	@Column(name = "id", length = 32)
	public String getId() {
		return id;
	}
	
	public void setId(String id) {
		this.id = id;
	}

	@Column(name = "serial_num", length = 50)
	public String getSerialNum() {
		return serialNum;
	}

	public void setSerialNum(String serialNum) {
		this.serialNum = serialNum;
	}
	
	@Column(name = "title", length = 255)
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Transient
	public String getWarehouseId() {
		return warehouseId;
	}

	public void setWarehouseId(String warehouseId) {
		this.warehouseId = warehouseId;
	}

	@Column(name = "type", length = 32)
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	
	@Transient
	public String getTypeVal() {
		return typeVal;
	}

	public void setTypeVal(String typeVal) {
		this.typeVal = typeVal;
	}

	@Column(name = "status", length = 5)
	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	@Column(name = "storage_time")
	public Date getStorageTime() {
		return storageTime;
	}

	public void setStorageTime(Date storageTime) {
		this.storageTime = storageTime;
	}
	
	@Transient
	public String getWriteUserId() {
		return writeUserId;
	}

	public void setWriteUserId(String writeUserId) {
		this.writeUserId = writeUserId;
	}

	@Transient
	public String getAuditUserId() {
		return auditUserId;
	}

	public void setAuditUserId(String auditUserId) {
		this.auditUserId = auditUserId;
	}

	@Column(name = "create_time")
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Column(name = "update_time")
	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}
	
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "warehouse_id")
	public MedicalWarehouse getWarehouse() {
		return warehouse;
	}

	public void setWarehouse(MedicalWarehouse warehouse) {
		this.warehouse = warehouse;
	}
	
	@Transient
	public String getWarehouseName() {
		return warehouseName;
	}

	public void setWarehouseName(String warehouseName) {
		this.warehouseName = warehouseName;
	}

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "write_user_id")
	public MedicalAdmin getWriteUser() {
		return writeUser;
	}

	public void setWriteUser(MedicalAdmin writeUser) {
		this.writeUser = writeUser;
	}
	
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "audit_user_id")
	public MedicalAdmin getAuditUser() {
		return auditUser;
	}

	public void setAuditUser(MedicalAdmin auditUser) {
		this.auditUser = auditUser;
	}

	@Column(name="hospital_id",length = 32)
	public String getHospitalId() {
		return hospitalId;
	}

	public void setHospitalId(String hospitalId) {
		this.hospitalId = hospitalId;
	}
	
	@Transient
	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String toJSONString() {
		JSONObject obj = new JSONObject();
		obj.put("id", id);
		obj.put("serialNum", this.serialNum);
		obj.put("title", title);
		obj.put("warehouseId", warehouse.getId());
		obj.put("warehouseName", warehouse.getName());
		obj.put("storageTime", DateFormatUtils.format(storageTime, "yyyy-MM-dd"));
		obj.put("type", type);
		obj.put("typeVal", typeVal);
		obj.put("writeUserName", writeUser.getName());
		if(null != auditUser) {
			obj.put("auditUserName", auditUser.getName());
		}
		obj.put("statusVal", status);
		if(SystemConfig.STORAGE_STATUS_PENDING.equals(status)){
			obj.put("status", "待审核");
		}else if(SystemConfig.STORAGE_STATUS_APPROVAL.equals(status)){
			obj.put("status", "已入库");
		}else if(SystemConfig.STORAGE_STATUS_REJECT.equals(status)) {
			obj.put("status", "审核未通过");
		}else if(SystemConfig.STORAGE_STATUS_NEW.equals(status)) {
			obj.put("status", "新建");
		}
		
		
		return obj.toString();
	}

}
